#
# TCSS 422 - Spring 2013
# Paul Ganea, Ralph Feltis, Tarl Hahn
# Team: Something Awesome (We can't remember the exact name)
# 5/2/2013
#

#
# Reports overall statistics about a web crawl.
#
import time

class Reporter(object): 
    def __init__(self):
        self.myBeans = []
        self.Keywords = {}
        self.pagecount= 0
        self.avgwordcount = 0
        self.avgurlscount = 0
        self.totalwordcount = 0
        self.totalurlcount = 0
        self.totalparsetime =0
        self.avgparsetime = 0
        self.startTime = time.clock()
        self.lastBeanAddTime = time.clock()
        
    def addBean(self, BEAN):
        self.myBeans.append(BEAN)
        self.pagecount+=1
        self.totalurlcount += BEAN.urlcount
        self.totalwordcount+= BEAN.wordcount
        self.avgwordcount= self.totalwordcount/len(self.myBeans)
        self.totalparsetime+=BEAN.time
        for KEY,VALUE in BEAN.KEYS.iteritems():
            try:
                self.Keywords[KEY]+=1
            except KeyError:
                self.Keywords[KEY] = VALUE
        self.lastBeanAddTime = time.clock()                
    
        
    def __str__(self):
        buffer = "Total word count: " + str(self.totalwordcount)
        buffer+= "\nPage count: " + str(self.pagecount)
        if self.lastBeanAddTime - self.startTime > 0.1:
            buffer+= "\nTotal running time: " + str(self.lastBeanAddTime - self.startTime)
        if self.pagecount > 0:
            buffer+= "\nAverage words per page: " + str(self.totalwordcount / float(self.pagecount))
            buffer+= "\nAverage URLs per page: " + str(self.totalurlcount / float(self.pagecount))
            buffer+= "\nAvg Parse time per page: " + str(self.totalparsetime / float(self.pagecount))
        buffer+= "\nKeywords and their count: "
        for KEY,VALUE in self.Keywords.iteritems():
            buffer+= "\n Keyword: " + KEY + " Avg hits per page: " + str(VALUE / float(self.pagecount))  + " Total hits: " + str(VALUE)
        return buffer
        
        
